{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7e43ed67-9fbb-4d6c-9a5d-8c4addeb2ed5",
   "metadata": {},
   "source": [
    "# Client Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b123c960-a0b4-4d5e-b15f-729de23974f5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langserve import RemoteRunnable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19dafdeb-63c5-4218-b0f9-fc20754369be",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from typing import Optional, List\n",
    "from pydantic import BaseModel, Field\n",
    "\n",
    "class Person(BaseModel):\n",
    "    age: Optional[int] = Field(None, description=\"The age of the person in years.\")\n",
    "    name: Optional[str] = Field(None, description=\"The name of the person.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf79ef88-b816-46aa-addf-9366b7ebdcaf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "runnable = RemoteRunnable(\"http://localhost:8000/extract_text/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5f102a5c-a80c-4480-863b-30f3aaad5afe",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "My name is Chester. I am 42 years old. My friend Jane is a year older than me.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "553d7dbc-9117-4834-83b1-11e28a513170",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': [{'name': 'Chester', 'age': 42}]}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = runnable.invoke({\"text\": text, \"schema\": Person.schema()})\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70d8d7c-5f0b-4757-92b7-cdd40f351275",
   "metadata": {},
   "source": [
    "Add instructions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "97294409-6daf-418d-9cbe-f44946245e35",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': [{'name': 'Chester', 'age': 42}]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "instructions = \"Redact all names using the characters `######`\"\n",
    "\n",
    "response = runnable.invoke(\n",
    "    {\n",
    "        \"text\": text,\n",
    "        \"schema\": Person.schema(),\n",
    "        \"instructions\": instructions,\n",
    "    }\n",
    ")\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b4a123-7841-465b-b43b-1db439c45fa7",
   "metadata": {},
   "source": [
    "Add few-shot examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bae9416d-abd4-4b41-90c2-3144c8566483",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': [{'name': '######', 'age': 42}, {'name': 'Jane', 'age': 43}]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "instructions = \"Redact all names using the characters `######`\"\n",
    "examples = [\n",
    "    {\n",
    "        \"text\": \"My name is Grung. I am 100.\",\n",
    "        \"output\": [ {\"age\": 100, \"name\": \"######\", \"hello\": \"meow\"}] ,\n",
    "    }\n",
    "]\n",
    "\n",
    "response = runnable.invoke(\n",
    "    {\n",
    "        \"text\": text,\n",
    "        \"schema\": Person.schema(),\n",
    "        \"instructions\": instructions,\n",
    "        \"examples\": examples,\n",
    "    }\n",
    ")\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83244e1a-7d4a-489d-b88c-b4e35ac76001",
   "metadata": {},
   "source": [
    "## Persist extractors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7fc60d58-edfd-4d2b-a71f-fc5e9c6ab58b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from uuid import uuid4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b1124672-ee4b-484a-be07-16687bb229e3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "url = \"http://localhost:8000\"\n",
    "user_id = str(uuid4())  # indicates owner for extractor\n",
    "headers = {\"x-key\": user_id}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a491b3e-999f-4f88-87f0-a282d582ef18",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\n",
    "    \"name\": \"people_extractor\",\n",
    "    \"description\": \"Extract references to people, having properties name and age.\",\n",
    "    \"schema\": Person.schema(),\n",
    "    \"instruction\": \"Redact all names using the characters `######`\",\n",
    "}\n",
    "\n",
    "response = requests.post(f\"{url}/extractors\", json=data, headers=headers)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1cf96c62-5653-4955-87ad-48ca009252d0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fa60ccce-5637-41b4-ba1a-085a56d0fa5b\n"
     ]
    }
   ],
   "source": [
    "uuid = response.json()['uuid']\n",
    "print(uuid)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7ddd7ea-31b6-49ad-a89a-8d5d7efa5f22",
   "metadata": {},
   "source": [
    "### Add examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d17f0283-a517-497f-9838-ade72f2e6359",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "examples = [\n",
    "    {\n",
    "        \"text\": \"My name is Grung. I am 100.\",\n",
    "        \"output\": [Person(age=100, name=\"######\").dict()],\n",
    "    }\n",
    "]\n",
    "\n",
    "responses = []\n",
    "for example in examples:\n",
    "    create_request = {\n",
    "        \"extractor_id\": uuid,\n",
    "        \"content\": example[\"text\"],\n",
    "        \"output\": example['output'],\n",
    "    }\n",
    "    response = requests.post(f\"{url}/examples\", json=create_request, headers=headers)\n",
    "    responses.append(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eb0b90d9-090a-4b84-9e31-e6c9f92de6ea",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = requests.get(f\"{url}/examples?extractor_id={uuid}\", headers=headers)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7b82eb80-bc44-455d-a8df-28616f94885d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'extractor_id': 'fa60ccce-5637-41b4-ba1a-085a56d0fa5b',\n",
       "  'content': 'My name is Grung. I am 100.',\n",
       "  'created_at': '2024-03-22T12:10:32.862261',\n",
       "  'updated_at': '2024-03-22T12:10:32.862265',\n",
       "  'output': [{'age': 100, 'name': '######'}],\n",
       "  'uuid': '94c8a41a-7d33-4795-a6be-23e72fe6c4e4'}]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dcf03bf-0b71-4c1f-a88a-bce090762d2c",
   "metadata": {},
   "source": [
    "### Extract using persisted extractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d5f84d0c-3d06-4d76-b9e5-c68e659ef930",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'extractor_id': 'fa60ccce-5637-41b4-ba1a-085a56d0fa5b', 'text': '\\nMy name is Chester. I am 42 years old. My friend Jane is a year older than me.\\n'}\n"
     ]
    }
   ],
   "source": [
    "request_data = {\"extractor_id\": uuid, \"text\": text}\n",
    "print(request_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d2bc2481-0dca-42aa-b3a7-d193721e149e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = requests.post(f\"{url}/extract\", data=request_data, headers=headers)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f389da36-5b43-41f5-b0a6-a389c6303937",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"data\":[{\"name\":\"######\",\"age\":42},{\"name\":\"######\",\"age\":43}]}'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.text"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
